/**
 * ViewListListener.java 2008-05-16
 * 
 * Linker is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package linker.gui;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

import linker.list.ListAction;
import linker.list.ListItem; 
import linker.list.ViewList;

/**
 * The listener of the list which can be viewed in 'MainWindow' 's viewList.Used
 * to catch the mouse event on list.
 * 
 * @version 2008-05-18
 * @author Jianfeng tujf.cn@gmail.com
 * @author Tan tan1986@gmail.com
 * 
 */

public class ListMouseListener extends MouseAdapter {

	/**
	 * The JList to be listened by the mouseListener.
	 */
	private ViewList viewList;

	/**
	 * The last displayMenu.
	 */
	private JPopupMenu lastMenu;

	/**
	 * The last saved mouse clicked point.
	 */
	private Point clickPoint = null;

	/**
	 * The action used to show the list's event.
	 */
	private ListAction listAction;

	/**
	 * @param action
	 *            The list's action.
	 * @param list
	 *            The list.
	 */
	public ListMouseListener(final ListAction action, final ViewList list) {
		this.listAction = action;
		this.viewList = list;
	}

	/**
	 * Mouse exited, hide the last popup menu.
	 * 
	 * @param e
	 *            MouseEvent.
	 */
	public final void mouseExited(final MouseEvent e) {
		if (lastMenu != null && clickPoint != null
				&& !e.getPoint().equals(clickPoint)) {
			lastMenu.setVisible(false);
		}
	}

	/**
	 * Mouse click event.
	 * 
	 * @param e
	 *            MouseEvent.
	 */
	public final void mouseClicked(final MouseEvent e) {
		int index = viewList.locationToIndex(e.getPoint());

		// Skip unavailble click, or set selected.
		if (index == -1) {
			return;
		} else {
			viewList.setSelectedIndex(index);
		}

		// Save clickpoint.
		clickPoint = e.getPoint();

		ListItem listItem = ((ListItem) viewList.getModel().getElementAt(
				index));

		if (SwingUtilities.isRightMouseButton(e)) {
			if (lastMenu != null) {
				lastMenu.setVisible(false);
			}

			lastMenu = null;

			// popupMenu is implemented by using state
			JPopupMenu popupMenu = listAction.getPopupMenu(listItem);

			if (popupMenu != null) {
				popupMenu.setLocation(e.getLocationOnScreen());
				popupMenu.setVisible(true);
				lastMenu = popupMenu;
			}
			return;
		}

		// double click
		if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {

			// make double working(not just sys.out, in buddyListState
			listAction.doubleClick(listItem);
		}
	}
}
